package com.chmingx.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.chmingx.springcloud.utils.CustomerBlockHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RateLimitController {

    @GetMapping("/byResource")
    @SentinelResource(value = "byResource", blockHandler = "handleException")
    public String byResource() {
        return "200, 按资源名称限流测试, OK";
    }

    public String handleExeception(BlockException blockException) {
        return "444, 按资源名称限流测试, Fail";
    }

    @GetMapping("/rateLimit/byUrl")
    @SentinelResource("byUrl")
    public String byUrl() {
        return "200, 按url限流测试, OK";
    }

    // 使用自定义的用户处理类
    @GetMapping("/rateLimit/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler",
            blockHandlerClass = CustomerBlockHandler.class, blockHandler = "handlerException2")
    public String customerBlockHandler() {
        return "200, 自定义限流处理, OK";
    }
}
